#!/user/bin/env python3
# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np

filename = r'D:\data\lena.jpg'
src = cv.imread(filename)
#获取图像的高、宽
h, w = src.shape[:2]
#求dx,dy
x_grad = cv.Sobel(src, cv.CV_32F, 1, 0)
y_grad = cv.Sobel(src, cv.CV_32F, 0, 1)
cv.imshow("x grade", x_grad)
cv.imshow("y grade", y_grad)
#讲32位的图像数据转换为8位的图像信息
x_grad = cv.convertScaleAbs(x_grad)
y_grad = cv.convertScaleAbs(y_grad)

dst = cv.add(x_grad, y_grad, dtype=cv.CV_16S)
dst = cv.convertScaleAbs(dst)
cv.imshow("gradient", dst)

result = np.zeros([h, w*2, 3], dtype=src.dtype)
result[0:h, 0:w, :] = src
result[0:h, w:2*w, :] = dst
cv.imshow("Sobel resylt", result)

#100、300分别低高阈值，一般为1:2或1:3
edge = cv.Canny(src, 100, 300)
cv.imshow("mask image", edge)
#讲边缘当成mask，与原图进行与预算，在原图显示出从彩色边缘
edge_src = cv.bitwise_and(src, src, mask=edge)
cv.imshow("canny result", edge_src)

cv.waitKey()
cv.destroyAllWindows()

